package source.events;

import source.model.type.UnitType;
import source.Driver;

/**
 * UserInputManager class acts as an Application-EventManager for the controller.  The
 * class has one method for each type of event that a user can specify from input.
 * UserInputManager is responsible for checking the validity of user requested events.
 * As long as the event is allowed by the current screen and current type of entity,
 * UserInputManager fires the event. 
 * @author Joe
 *
 */
public class UserInputManager {

	//static constants for which screen is visible currently
	public static final int AREAVIEWPORT = 0;
	public static final int UNITOVERVIEW = 1;
	public static final int STRUCTUREOVERVIEW = 2;
	public static final int TECHTREE = 3;
		
	private int currentScreen;
	private int pID;
	private EventHandler<ModelEvent> eh;
	
	public UserInputManager(int pID, EventHandler<ModelEvent> eh)
	{
		this.pID = pID;
		this.eh = eh;
		currentScreen = AREAVIEWPORT;
	}
	
	/**
	 * @param s: int, the integer representation of the new screen
	 */
	
	public int getPID()
	{
		return pID;
	}
	
	public void setScreen(int s)
	{
		switch(s)
		{
			case 0:
				currentScreen = AREAVIEWPORT;
				break;
		
			case 1:
				currentScreen = UNITOVERVIEW;
				break;
			
			case 2:
				currentScreen = STRUCTUREOVERVIEW;
				break;
				
			case 3:
				currentScreen = TECHTREE;
		}
	}
	
	public void selectNextUnit()
	{
	  if(currentScreen == AREAVIEWPORT)
	  {
		eh.fireEventNow(new ModelEvent(pID, ModelEventType.SELECTNEXTUNIT));
	  }
	}
	
	public void selectNextBase()
	{
	  if(currentScreen == AREAVIEWPORT)
	  {
		eh.fireEventNow(new ModelEvent(pID, ModelEventType.SELECTNEXTBASE));
	  }
	}
	
	public void endTurn()
	{
	  if(currentScreen == AREAVIEWPORT){
		  pID = (pID + 1)%Driver.numPlayers;
	  }
		eh.fireEventNow(new ModelEvent(pID, ModelEventType.ENDTURN));
	}
	
	/**
	 * @param keyMap: String[]
	 */
	
	public void showUserKeysScreen(String[] keyMap)
	{
		
	}
	
	public void changeUserKeys()
	{
		
	}
	
	/**
	 * @param direction: int, the direction we want to move the cursor
	 */
	
	public void moveCursor(int direction)
	{

	}
	
	/**
	 * @param direction: int, the direction we want to move the unit
	 */
	
	public void moveUnit(int direction)
	{
	  if(currentScreen == AREAVIEWPORT)
	  {
			switch(direction)
			{
				case 8:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVEN));
					break;
					
				case 9:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVENE));
					break;
					
				case 6:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVEE));
					break;
					
				case 3:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVESE));
					break;
					
				case 2:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVES));
					break;
					
				case 1:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVESW));
					break;
					
				case 4:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVEW));
					break;
					
				case 7:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.MOVENW));
					break;
			}
	  }
	}
	
	public void unitDoesNothing()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.UNITDOESNOTHING));
	}
	
	public void attack()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.ATTACK));
	}
	
	public void removeTrap()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.REMOVETRAP));
	}
	
	public void setTrap()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.SETTRAP));
	}
	public void observe()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.OBSERVE));
	}
	
	public void sneak()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.SNEAK));
	}
	
	public void pickupItem()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.PICKUPITEM));
	}

	public void dropItem()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.DROPITEM));		
	}
	
	public void useItem()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.USEITEM));	
	}
	
	public void healUnit()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.HEALUNIT));	
	}
	
	public void toggleMission()
	{
	  if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.TOGGLEMISSION));
	}
	
	public void toggleFreeRoam()
	{
		if(currentScreen == AREAVIEWPORT)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.TOGGLEFREEROAM));
	}
	/**
	 * @param uType: UnitType, the unit type we want to build
	 */
	
	public void buildUnit(UnitType uType)
	{
	  if(currentScreen == AREAVIEWPORT)
	  {
			switch(uType)
			{
				case COLONIST:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.BUILDCOLONIST));				
					break;
					
				case EXPLORER:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.BUILDEXPLORER));				
					break;
					
				case MELEE:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.BUILDMELEE));				
					break;
					
				case RANGED:
					eh.fireEventNow(new ModelEvent(pID, ModelEventType.BUILDRANGED));				
					break;
			}
	  }
	}
	
	public void buildBase()
	{
	  if(currentScreen == AREAVIEWPORT)
		  eh.fireEventNow(new ModelEvent(pID, ModelEventType.BUILDBASE));
	}
	
	public void startReceivingRallyPointCommands()
	{
		
	}
	
	public void endReceivingRallyPointCommands()
	{
		
	}
	
	public void assembleArmy()
	{
		if(currentScreen == UNITOVERVIEW)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.MAKEARMY));	
	}
	
	public void disbandArmy()
	{
		if(currentScreen == UNITOVERVIEW)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.DISBANDARMY));	
	}
	
	public void assembleBaseGroup()
	{
		
	}
	
	public void disbandBaseGroup()
	{
		
	}
	
	public void selectResearchTech()
	{
		if(currentScreen == TECHTREE)
			eh.fireEventNow(new ModelEvent(pID, ModelEventType.SELECTRESEARCHTECH));	
		
	}
	

	
}


